//
//  LGConsultRecordDiseaseCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/18.
//  咨询记录 - 共病类型

#import "LGConsultRecordDiseaseCell.h"

#define placeHolder  @"请填写其他疾病信息"

@interface LGConsultRecordDiseaseCell()<UITextViewDelegate>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**  高血压 */
@property (nonatomic , strong)UIButton *pressureBtn;
/**   */
@property (nonatomic , strong)UITextField *pressureTF;
/**   */
@property (nonatomic , strong)UILabel *pressureUnit;
/**  糖尿病 */
@property (nonatomic , strong)UIButton *glucoseBtn;
/**   */
@property (nonatomic , strong)UITextField *glucoseTF;
/**   */
@property (nonatomic , strong)UILabel *glucoseUnit;
/**  陈旧性心梗 */
@property (nonatomic , strong)UIButton *attackBtn;
/**  心衰 */
@property (nonatomic , strong)UIButton *failureBtn;
/**  肾功能不全或肾衰竭 */
@property (nonatomic , strong)UIButton *renalBtn;
/**   */
@property (nonatomic , strong)UITextView *contentTF;
/**   */
@property (nonatomic , strong)NSArray *titleArray;
/**   */
@property (nonatomic , copy)NSString *content;

@end

@implementation LGConsultRecordDiseaseCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setBaseMargin:(CGFloat)baseMargin{
    _baseMargin = baseMargin;
    [self.baseView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(baseMargin);
        make.right.equalTo(self.contentView).offset(-baseMargin);
    }];
}

-(void)setResultArray:(NSArray *)resultArray{
    _resultArray = resultArray;
    NSString *content = @"";
    self.pressureBtn.selected = NO;
    self.pressureTF.text = @"";
    self.glucoseBtn.selected = NO;
    self.glucoseTF.text = @"";
    self.attackBtn.selected = NO;
    self.failureBtn.selected = NO;
    self.renalBtn.selected = NO;
    for (NSInteger i=0; i<resultArray.count; i++) {
        NSString *title = LGNSString(resultArray[i][@"title"]);
        NSString *year = LGNSString(resultArray[i][@"year"]);
        if([title isEqualToString:@"高血压"]){
            self.pressureBtn.selected = YES;
            self.pressureTF.text = year;
            
        }else if ([title isEqualToString:@"糖尿病"]){
            self.glucoseBtn.selected = YES;
            self.glucoseTF.text = year;
            
        }else if ([title isEqualToString:@"陈旧性心梗"]){
            self.attackBtn.selected = YES;
            
        }else if ([title isEqualToString:@"心衰"]){
            self.failureBtn.selected = YES;
        }else if ([title isEqualToString:@"肾功能不全或肾衰竭"]){
            self.renalBtn.selected = YES;
        }else{
            content = title;
        }
    }
    self.content = content;
}

-(void)caculateSelectResult{
    NSMutableArray *resultArray = [NSMutableArray array];
    if(self.pressureBtn.selected == YES){
        [resultArray addObject:@{@"title":@"高血压",@"year":self.pressureTF.text}];
    }
    if(self.glucoseBtn.selected == YES){
        [resultArray addObject:@{@"title":@"糖尿病",@"year":self.glucoseTF.text}];
    }
    if(self.attackBtn.selected == YES){
        [resultArray addObject:@{@"title":@"陈旧性心梗",@"year":@""}];
    }
    if(self.failureBtn.selected == YES){
        [resultArray addObject:@{@"title":@"心衰",@"year":@""}];
    }
    if(self.renalBtn.selected == YES){
        [resultArray addObject:@{@"title":@"肾功能不全或肾衰竭",@"year":@""}];
    }
    if(self.content.length>0 && ![self.content isEqualToString:placeHolder]){
        [resultArray addObject:@{@"title":self.content,@"year":@""}];
    }
    if(self.delegate && [self.delegate respondsToSelector:@selector(diseaseItemSelect:)]){
        [self.delegate diseaseItemSelect:resultArray];
    }
}

-(void)itemButtonTouched:(UIButton *)sender{
    sender.selected = !sender.selected;
    [self caculateSelectResult];
}

-(void)setContent:(NSString *)content{
    _content = content;
    if(content.length == 0 || [content isEqualToString:placeHolder]){
        self.contentTF.font = LGFont(14);
        self.contentTF.textColor = [UIColor colorWithString:@"#999999"];
        self.contentTF.attributedText = [placeHolder attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#999999"]} alignment:NSTextAlignmentLeft lineSpacing:5];
    }else{
        self.contentTF.font = LGFont(15);
        self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
        self.contentTF.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} alignment:NSTextAlignmentLeft lineSpacing:5];
    }
    [self performSelector:@selector(refreshContentHeight) withObject:nil afterDelay:0.1];
    
}

-(void)refreshContentHeight{
    CGFloat contentW = self.contentTF.bounds.size.width;
    CGFloat height = [self.contentTF.text textHeightWithFont:LGFont(14) maxWidth:contentW lineSpacing:5];
    height = (height>=viewPix(36))?height:viewPix(36);
    [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height));
    }];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:placeHolder]){
        self.contentTF.text = @"";
    }
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    self.content = textView.text;
    [self caculateSelectResult];
}

- (void)textViewDidChange:(UITextView *)textView{
    //键盘输入模式
    NSString *lang = textView.textInputMode.primaryLanguage;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) {
            //没有高亮
            [self handleTextViewText:textView.text];
        }
    }else{
        [self handleTextViewText:textView.text];
    }
}

-(void)handleTextViewText:(NSString *)content{
//    content = (content.length > 20)?[content substringToIndex:20]:content;
    if(content.length>0){
        self.contentTF.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} alignment:NSTextAlignmentLeft lineSpacing:5];
    }
    CGSize maxSize = CGSizeMake(self.contentTF.size.width, MAXFLOAT);
    CGSize fitSize = [self.contentTF sizeThatFits:maxSize];
    CGFloat viewHeight = self.contentTF.bounds.size.height;
    CGFloat newHeight = fitSize.height;
    newHeight = (newHeight>=viewPix(36))?newHeight:viewPix(36);
    NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
    [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(newHeight));
    }];
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

-(void)textFieldEditingChanged:(UITextField *)textField{
    textField.text = (textField.text.length>2)?[textField.text substringToIndex:2]:textField.text;
    [self caculateSelectResult];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.baseView];
    [self.baseView addSubview:self.titleLabel];
    [self.baseView addSubview:self.pressureBtn];
    [self.baseView addSubview:self.pressureTF];
    [self.baseView addSubview:self.pressureUnit];
    [self.baseView addSubview:self.glucoseBtn];
    [self.baseView addSubview:self.glucoseTF];
    [self.baseView addSubview:self.glucoseUnit];
    [self.baseView addSubview:self.attackBtn];
    [self.baseView addSubview:self.failureBtn];
    [self.baseView addSubview:self.renalBtn];
    [self.baseView addSubview:self.contentTF];
    [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
    [self.pressureTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.pressureBtn.mas_right).offset(viewPix(26));
        make.right.equalTo(self.pressureUnit.mas_left);
        make.centerY.equalTo(self.pressureBtn);
        make.height.equalTo(@(viewPix(32)));
    }];
    [self.pressureUnit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.centerY.equalTo(self.pressureBtn);
        make.width.equalTo(@(viewPix(22)));
    }];
    
    [self.glucoseTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.glucoseBtn.mas_right).offset(viewPix(26));
        make.right.equalTo(self.glucoseUnit.mas_left);
        make.centerY.equalTo(self.glucoseBtn);
        make.height.equalTo(@(viewPix(32)));
    }];
    [self.glucoseUnit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.centerY.equalTo(self.glucoseBtn);
        make.width.equalTo(@(viewPix(22)));
    }];
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.baseView).offset(viewPix(215));
        make.left.equalTo(self.baseView).offset(viewPix(12));
        make.right.equalTo(self.baseView).offset(-viewPix(12));
        make.bottom.equalTo(self.baseView).offset(-viewPix(6));
        make.height.equalTo(@(viewPix(36)));
    }];
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"共病类型" colorString:@"#333333" textFont:LGFontWeight(15, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _titleLabel.frame = CGRectMake(viewPix(12), viewPix(6), viewPix(100), viewPix(20));
    }
    return _titleLabel;
}

-(UIButton *)pressureBtn{
    if(!_pressureBtn){
        _pressureBtn = [UIButton buttonWithTitle:@"高血压" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _pressureBtn.frame = CGRectMake(viewPix(12), kMaxY(self.titleLabel.frame)+viewPix(12), viewPix(100), viewPix(32));
        _pressureBtn.cornerRidus = viewPix(16);
        [_pressureBtn setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_pressureBtn setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [_pressureBtn addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _pressureBtn;
}

-(UITextField *)pressureTF{
    if(!_pressureTF){
        _pressureTF = [[UITextField alloc]init];
        _pressureTF.tintColor = [UIColor colorWithString:@"#333333"];
        _pressureTF.textColor = [UIColor colorWithString:@"#333333"];
        _pressureTF.textAlignment = NSTextAlignmentCenter;
        _pressureTF.keyboardType = UIKeyboardTypeNumberPad;
        _pressureTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _pressureTF.layer.borderWidth = 1;
        _pressureTF.cornerRidus = 5;
        _pressureTF.placeholder = @"请填写年限";
        _pressureTF.font = LGFont(14);
        [_pressureTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _pressureTF;
}

-(UILabel *)pressureUnit{
    if(!_pressureUnit){
        _pressureUnit = [UILabel lableWithText:@"年" colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _pressureUnit;
}

-(UIButton *)glucoseBtn{
    if(!_glucoseBtn){
        _glucoseBtn = [UIButton buttonWithTitle:@"糖尿病" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _glucoseBtn.frame = CGRectMake(viewPix(12), kMaxY(self.pressureBtn.frame)+viewPix(12), viewPix(100), viewPix(32));
        _glucoseBtn.cornerRidus = viewPix(16);
        [_glucoseBtn setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_glucoseBtn setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [_glucoseBtn addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _glucoseBtn;
}


-(UITextField *)glucoseTF{
    if(!_glucoseTF){
        _glucoseTF = [[UITextField alloc]init];
        _glucoseTF.tintColor = [UIColor colorWithString:@"#333333"];
        _glucoseTF.textColor = [UIColor colorWithString:@"#333333"];
        _glucoseTF.textAlignment = NSTextAlignmentCenter;
        _glucoseTF.keyboardType = UIKeyboardTypeNumberPad;
        _glucoseTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _glucoseTF.layer.borderWidth = 1;
        _glucoseTF.cornerRidus = 5;
        _glucoseTF.placeholder = @"请填写年限";
        _glucoseTF.font = LGFont(14);
        [_glucoseTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _glucoseTF;
}

-(UILabel *)glucoseUnit{
    if(!_glucoseUnit){
        _glucoseUnit = [UILabel lableWithText:@"年" colorString:@"#333333" textFont:LGFont(14) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _glucoseUnit;
}

-(UIButton *)attackBtn{
    if(!_attackBtn){
        _attackBtn = [UIButton buttonWithTitle:@"陈旧性心梗" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _attackBtn.frame = CGRectMake(viewPix(12), kMaxY(self.glucoseBtn.frame)+viewPix(12), viewPix(100), viewPix(32));
        _attackBtn.cornerRidus = viewPix(16);
        [_attackBtn setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_attackBtn setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [_attackBtn addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _attackBtn;
}

-(UIButton *)failureBtn{
    if(!_failureBtn){
        _failureBtn = [UIButton buttonWithTitle:@"心衰" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _failureBtn.frame = CGRectMake(kMaxX(self.attackBtn.frame)+viewPix(8), kMinY(self.attackBtn.frame), viewPix(100), viewPix(32));
        _failureBtn.cornerRidus = viewPix(16);
        [_failureBtn setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_failureBtn setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [_failureBtn addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _failureBtn;
}

-(UIButton *)renalBtn{
    if(!_renalBtn){
        _renalBtn = [UIButton buttonWithTitle:@"肾功能不全或肾衰竭" titleFont:LGFontWeight(15, UIFontWeightMedium) normalColor:@"#666666" selectColor:@"#FFFFFF"];
        _renalBtn.frame = CGRectMake(viewPix(12), kMaxY(self.attackBtn.frame)+viewPix(12), viewPix(175), viewPix(32));
        _renalBtn.cornerRidus = viewPix(16);
        [_renalBtn setBackgroundColor:[UIColor colorWithString:@"#F6F6F6"] forState:UIControlStateNormal];
        [_renalBtn setBackgroundColor:[UIColor colorWithString:@"#3291FF"] forState:UIControlStateSelected];
        [_renalBtn addTarget:self action:@selector(itemButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _renalBtn;
}

-(UITextView *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextView alloc]init];
        _contentTF.textContainerInset = UIEdgeInsetsMake(viewPix(10), viewPix(8), viewPix(10), viewPix(6));
        _contentTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _contentTF.tintColor = [UIColor colorWithString:@"#333333"];
        _contentTF.textColor = [UIColor colorWithString:@"#333333"];
        _contentTF.keyboardType = UIKeyboardTypeDefault;
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.layer.borderWidth = 1;
        _contentTF.font = LGFont(14);
        _contentTF.delegate = self;
        _contentTF.cornerRidus = 5;
        _contentTF.scrollEnabled = NO;
    }
    return _contentTF;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
